package cn.kone.blog.controller;


import cn.kone.base.utlis.R;
import cn.kone.blog.domain.ArticleType;
import cn.kone.blog.domain.query.ArticleTypeQuery;
import cn.kone.blog.dto.ArticleTypeDTO;
import cn.kone.blog.service.IArticleTypeService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articleType")
@Api(tags = "文章类型接口类")
public class ArticleTypeController {
    @Autowired
    private IArticleTypeService articleTypeService;

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部")
    public R<List<ArticleType>> findAllArticleTypes() {

        List<ArticleType> all = articleTypeService.findAll();
        return R.ok(all);
    }

    /**
     * 查询单个
     *
     * @param id 用户id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public R<ArticleType> findOneArticleTypes(@PathVariable("id") Long id) {
        ArticleType one = articleTypeService.findOne(id);
        return R.ok(one);
    }

   @PutMapping
   @ApiOperation(value = "新增和修改")
   public R<Void> addAndUpdateArticleType(@RequestBody ArticleType articleType){
       Long id = articleType.getId();
       try {
           if (id==null){
               articleTypeService.addArticleType(articleType);
           }else {
               articleTypeService.updateArticleType(articleType);
           }
       } catch (Exception e) {
           e.printStackTrace();
           return R.fail();
       }
       return R.ok();
   }

    /**
     * 删除单个
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个")
    public R<Void> deleteArticleTypes(@PathVariable("id") Long id) {
        try {
            articleTypeService.deleteArticleType(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    /**
     * 分页查询
     * @param articleTypeQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageInfo<ArticleType> pageQuery(@RequestBody ArticleTypeQuery articleTypeQuery) {

        List<ArticleType> list = articleTypeService.pageQuery(articleTypeQuery);
        return new PageInfo<ArticleType>(list);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public R<Void> batchDeleteArticleType(@RequestBody Long[] ids){
        try {
            articleTypeService.batchDeleteArticleType(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    /**
     * 查询类型树
     * @param id 前端传过来的文章类型ID
     * @return
     */
    @GetMapping("/typeTree/{id}")
    @ApiOperation(value = "查询父类型")
    public List<ArticleType> typeTree(@PathVariable("id") Long id){
        //查询所有类型
        List<ArticleType> list = articleTypeService.findAll();

        return articleTypeService.typeTree(id,list);
    }


    @GetMapping("/articleTypeStatistics")
    @ApiOperation(value = "统计文章类型")
    public List<ArticleTypeDTO> articleTypeStatistics(){
        return articleTypeService.articleTypeStatistics();
    }


}
